/*
 * TimeStamp.java
 */
import java.util.concurrent.TimeUnit;

public class TimeStamp {
	
	private long startTime;
	private long stopTime;
	private boolean stopped = false;
	private TimeUnit ts;

	public TimeStamp() {
		this(TimeUnit.NANOSECONDS);
	}// end default constructor

	public TimeStamp(TimeUnit ts) {
		this.ts = ts;
		start();
	}// end overload constructor

	public void start() {
		startTime = System.nanoTime();
		stopped = false;
	}// end method start

	public void stop() {
		stopTime = System.nanoTime();
		stopped = true;
	}// end method stop

	public long elapsedTime() {
		if (!stopped)
			throw new IllegalStateException("Timestamp not stopped");
		return ts.convert(stopTime - startTime, TimeUnit.NANOSECONDS);
	}// end method elapsedTime

	public String toString() {
		try {
			return elapsedTime() + " " + ts;
		} catch (IllegalStateException ise) {
			return "Timestamp (not stopped)";
		}
	}// end method toString

	public String units() {
		return ts.toString();
	}// end method units
}// end class TimeStamp
